﻿using Sasc.PV.BLL;
using Sasc.PV.Common.DEncrypt;
using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sasc.PV.Web.Admin
{
    public partial class Index : PVAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //显示自定义导航快捷菜单
                this.DaohangListLabel.Text = DbHelperSQL.GetSHSL("select top 1 DaoHangList from ERPUser where UserName='" + PublicMethod.GetSessionValue("UserName") + "'");

                //绑定所有s的菜单项目
                BindTree(ListTreeView.Nodes, 0);
                //设置有权限的项才显示
                SetQuanXian();

                //显示授权信息文字
                this.ShouquanXinxiLabel.Text = DESEncrypt.Decrypt(DbHelperSQL.GetSHSL("select top 1 DanWeiStr from ERPSerils"), "www.ework100.com");

                //try
                //{
                //    this.ListTreeView.Nodes[0].Expanded = true;
                //}
                //catch
                //{ }
            }
        }
        private void BindTree(TreeNodeCollection Nds, int IDStr)
        {
            var MYDT = DbHelperSQL.GetDataSet("select * from ERPTreeList where ParentID=" + IDStr.ToString() + " order by PaiXuStr asc,ID asc");
            for (int i = 0; i < MYDT.Tables[0].Rows.Count; i++)
            {
                //if (MYDT.Tables[0].Rows[i]["NavigateUrlStr"].ToString().Trim().Length<=0||PublicMethod.StrIFIn("|" + MYDT.Tables[0].Rows[i]["ValueStr"].ToString() + "|", PublicMethod.GetSessionValue("QuanXian")) == true)
                //{
                TreeNode MenuNode = new TreeNode();
                MenuNode.Text = MYDT.Tables[0].Rows[i]["TextStr"].ToString();
                MenuNode.Value = MYDT.Tables[0].Rows[i]["ValueStr"].ToString();
                int strId = int.Parse(MYDT.Tables[0].Rows[i]["ID"].ToString());
                MenuNode.ImageUrl = MYDT.Tables[0].Rows[i]["ImageUrlStr"].ToString();

                if (MYDT.Tables[0].Rows[i]["NavigateUrlStr"].ToString().Trim().Length <= 0)
                {
                    MenuNode.SelectAction = TreeNodeSelectAction.Expand;
                }
                else
                {
                    MenuNode.NavigateUrl = MYDT.Tables[0].Rows[i]["NavigateUrlStr"].ToString();
                    MenuNode.Target = MYDT.Tables[0].Rows[i]["Target"].ToString();
                }
                Nds.Add(MenuNode);
                BindTree(Nds[Nds.Count - 1].ChildNodes, strId);
                //}            
            }
        }
        public void SetQuanXian()
        {
            if (PublicMethod.IsSuperAdmin())
                return;

            //判断权限分配
            for (int i = 0; i < this.ListTreeView.Nodes.Count; i++)
            {
                for (int j = 0; j < this.ListTreeView.Nodes[i].ChildNodes.Count; j++)
                {
                    //删除子菜单中的不在权限中的项
                    for (int k = 0; k < this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Count; k++)
                    {
                        if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k].Value + "|", PublicMethod.GetSessionValue("QuanXian")) == false)
                        {
                            this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes[k]);
                            k = -1;
                        }
                    }
                    //判断是父节点还是子节点
                    if (this.ListTreeView.Nodes[i].ChildNodes[j].SelectAction == TreeNodeSelectAction.Expand)
                    {
                        if (this.ListTreeView.Nodes[i].ChildNodes[j].ChildNodes.Count <= 0)
                        {
                            this.ListTreeView.Nodes[i].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j]);
                            j = -1;
                        }
                    }
                    else
                    {
                        if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].ChildNodes[j].Value + "|", PublicMethod.GetSessionValue("QuanXian")) == false)
                        {
                            this.ListTreeView.Nodes[i].ChildNodes.Remove(this.ListTreeView.Nodes[i].ChildNodes[j]);
                            j = -1;
                        }
                    }
                }
                //判断是父节点还是子节点
                if (this.ListTreeView.Nodes[i].SelectAction == TreeNodeSelectAction.Expand)
                {
                    if (this.ListTreeView.Nodes[i].ChildNodes.Count <= 0)
                    {
                        this.ListTreeView.Nodes.Remove(this.ListTreeView.Nodes[i]);
                        i = -1;
                    }
                }
                else
                {
                    if (PublicMethod.StrIFIn("|" + this.ListTreeView.Nodes[i].Value + "|", PublicMethod.GetSessionValue("QuanXian")) == false)
                    {
                        this.ListTreeView.Nodes.Remove(this.ListTreeView.Nodes[i]);
                        i = -1;
                    }
                }
            }
        }
    }
}